package cn.akira.domain.activity.service.partake;

import cn.akira.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import cn.akira.domain.activity.model.entity.ActivityEntity;
import cn.akira.domain.activity.model.entity.PartakeRaffleActivityEntity;
import cn.akira.domain.activity.model.entity.UserRaffleOrderEntity;
import cn.akira.domain.activity.model.valobj.ActivityStateVO;
import cn.akira.domain.activity.repository.IActivityRepository;
import cn.akira.domain.activity.service.IRaffleActivityPartakeService;
import cn.akira.types.enums.ResponseCode;
import cn.akira.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;
import java.util.Date;

/**
 * @author fjl95
 * @date 2025/6/5
 * @description
 */
@Slf4j
public abstract class AbstractRaffleActivityPartake implements IRaffleActivityPartakeService {

    @Resource
    private IActivityRepository activityRepository;




    @Override
    public UserRaffleOrderEntity createOrder(String userId, Long activityId) {
        return createOrder(PartakeRaffleActivityEntity.builder()
                .userId(userId)
                .activityId(activityId)
                .build());
    }

    @Override
    public UserRaffleOrderEntity createOrder(PartakeRaffleActivityEntity partakeRaffleActivityEntity) {

        //1.获得传参基础信息
        String userId = partakeRaffleActivityEntity.getUserId();
        Long activityId = partakeRaffleActivityEntity.getActivityId();
        Date currentDate = new Date();

        //2.活动查询
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);

        //3活动校验
        //活动状态
        if(!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        //活动日期
        if (activityEntity.getBeginDateTime().after(currentDate) || activityEntity.getEndDateTime().before(currentDate)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        //4.查询未被使用的活动参与订单记录,若存在，则返回该记录
        UserRaffleOrderEntity userRaffleOrderEntity = activityRepository.queryNoUsedRaffleOrder(partakeRaffleActivityEntity);
        if (null != userRaffleOrderEntity) {
            log.info("创建参与活动订单 userId:{} activityId:{} userRaffleOrderEntity:{}", userId, activityId, JSON.toJSONString(userRaffleOrderEntity));
            return userRaffleOrderEntity;

        }

         //5.账户额度校验&返回账户构建对象
        CreatePartakeOrderAggregate createPartakeOrderAggregate = this.doFilterAccount(userId,activityId,currentDate);

        //6.构建订单对象
        UserRaffleOrderEntity userRaffleOrder = this.buildUserRaffleOrder(userId, activityId, currentDate);

        //7.填充订单对象进聚合对象
        createPartakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrder);

        //8.保存聚合对象入库
        activityRepository.saveCreatePartakeOrderAggregate(createPartakeOrderAggregate);

        //9.返回订单信息
        return userRaffleOrder;
    }
    protected abstract CreatePartakeOrderAggregate doFilterAccount(String userId, Long activityId, Date currentDate);
    protected abstract UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate);


}
